package com.cisetech.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cisetech.service.LoginService;
import com.cisetech.service.ResourcesService;
import com.cisetech.service.UserService;
import com.cisetech.vo.bobocai.LResources;
import com.cisetech.vo.bobocai.LUser;
import com.cisetech.util.CreateMD5;
import com.cisetech.util.html.ResourcesHtmlUtil;
//登录逻辑层
@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	UserService userserviceImpl;
	@Autowired
	ResourcesService resourcesServiceImpl;
	
	@Override
	public String login(LUser user,HttpServletRequest request) {
		//判断用户是否存在
		if(user!=null && user.getName()!=null && !"".equals(user.getName())){
			List<LUser> list = userserviceImpl.findList(user);
			if(list==null || list.size()==0){
				return "1";//用户不存在
			}
			if(!(CreateMD5.getMd5(user.getPassword()).equals(list.get(0).getPassword()))){
				return "2";//密码不正确
			}
			//登录时将用户和用户权限（按钮）设置到session中
			userSetSession(list.get(0),request);
		}
		return "0";
	}
	//将用户放到session中
	public void userSetSession(LUser user,HttpServletRequest request) {
		request.getSession().setAttribute("luser", user);
		request.getSession().setAttribute("resourceshtml", getresources(user,request));
	}
	//将按钮（用户权限）放到session中
	public String getresources(LUser user,HttpServletRequest request){
		user.setAround("0");
		List<LResources> list = resourcesServiceImpl.findResourcesList(user);
		StringBuffer html = ResourcesHtmlUtil.getResourcesHtml(request,list);
		System.out.println(html.toString());
		return html.toString();
	}
}
